package com.pms.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.pms.entity.Contact;
import com.pms.entity.Data;
import com.pms.entity.Member;
import com.pms.entity.Project;
import com.pms.entity.Record;
import com.pms.util.DBHelp;
import com.pms.util.DateUtil;
import com.pms.util.PkUtil;
import com.pms.util.RowMapper;

public class RecordDao {

	DBHelp<Record> db = new DBHelp<Record>();
	
	public class RecordRowMapper implements RowMapper<Record>{

		@Override
		public Record mapRow(ResultSet rs) throws SQLException {
			
			Record record = new Record();
			record.setId(rs.getString("id"));
			record.setContent(rs.getString("content"));
			record.setTime(rs.getString("time"));
			record.setContactId(rs.getString("t_contact_id"));
			record.setProjectId(rs.getString("t_project_id"));
			record.setMemberId(rs.getString("t_member_id"));
			
			String projectId = rs.getString("t_project_id");
			String memberId = rs.getString("t_member_id");
			String contactId =rs.getString("t_contact_id");
			MemberDao memberDao = new MemberDao();
			Member member = memberDao.findById(memberId);
			record.setMember(member);
			
			ProjectDao projectDao = new ProjectDao();
			Project project = projectDao.findById(projectId);
			record.setProject(project);
			
			ContactDao contactDao = new ContactDao();
			Contact contact = contactDao.findContactById(contactId);
			record.setContact(contact);
			return record;
		}
		
	}

	public List<Record> findListByContactId(String contactId) {
		String sql = "select id ,content,`time`,t_member_id,t_project_id,t_contact_id from t_record where t_contact_id = ? order by time desc";
		return db.executeQueryForList(sql, new RecordRowMapper(), contactId);
	}

	public void newRecord(String content, String projectId, String memberId,
			String contactId) {
		String sql = "INSERT INTO t_record (id,content,`time`,t_member_id,t_project_id,t_contact_id)VALUES (?,?,?,?,?,?);";
		
		db.executeSQL(sql,PkUtil.getPk(),content,DateUtil.getNow(),memberId,projectId,contactId);
	}
	
	
}
